import * as PIXI from "pixi.js";

import {storeToRefs} from "pinia";
import  {usePixiStore}  from '@/store/pixi'
import { toRaw } from 'vue'

// 创建 应用
export class PixiApp{
    constructor() {
        this.app = null;
        this.renderer = null;
        this.create()
    }


    create(){
        let store = usePixiStore()
        let {currentWidth,currentHeight } = storeToRefs(store)
        store.changeCurrentWidth()
        store.changeCurrentHeight()

        this.app = new PIXI.Application({
            width: currentWidth.value,
            height: currentHeight.value,
            resolution: window.devicePixelRatio || 2,
            // resizeTo: window,
            antialias: true,     // 消除锯齿
            transparent: true,
            background: 'transparent',
            backgroundAlpha: 0,  // 背景不透明
            // clearBeforeRender:true,
            forceCanvas: true,
            autoDensity: true // 这属性很关键 模糊的处理
        });
        this.renderer = this.app.renderer;
    }

    static init(){
        // return  new PixiApp()

        let store = usePixiStore()
        let { _pixiApp } = store
        if(_pixiApp){
            return toRaw(_pixiApp)
        }else{
            store.changePixiApp(new PixiApp())
            return toRaw(store._pixiApp)
        }
    }
}

